package de.unistuttgart.iaaskpimonitoring.esper.eventmanagement;

import javax.jms.Message;

import com.espertech.esper.client.EPServiceProviderManager;

public abstract class FilterElement {
	protected FilterElement successor;
	protected Streamer eventStreamer;
	protected String targetEngineURI;

	
	public Streamer getEventStreamer() {
		return eventStreamer;
	}

	public FilterElement getSuccessor() {
		return successor;
	}

	public void setSuccessor(FilterElement successor) {
		this.successor = successor;
	}

	public void filterMessage(Message msg){
		String eventType = matchMsg(msg);
		if (eventType == null) {
			if (successor != null) {
				successor.filterMessage(msg);
			}
		} else {
			eventStreamer.sendEvent(eventStreamer.createEvent(msg, eventType),
					EPServiceProviderManager.getProvider(targetEngineURI));
		}
	}

	//Return the type of the event otherwise null
	public abstract String matchMsg(Message msg);

	public String getTargetEngineURI() {
		return targetEngineURI;
	}

	public void setTargetEngineURI(String targetEngineURI) {
		this.targetEngineURI = targetEngineURI;
		if (successor != null)
			successor.setTargetEngineURI(targetEngineURI);
	}

}
